package ces.lawsuit.interceptor;

import java.util.HashSet;
import java.util.Set;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;

import ces.lawsuit.model.User;
import ces.lawsuit.util.Util;

public class CommonInterceptor implements Interceptor {
private Set<String> methodSet = new HashSet<String>();
	
	public CommonInterceptor(String... methods) {
		if (methods == null || methods.length == 0)
			throw new IllegalArgumentException("methods can not be null.");
		
		for (String method : methods)
			methodSet.add(method.trim());
	}
	@Override
	public void intercept(Invocation inv) {
		// TODO Auto-generated method stub
		if (methodSet.contains(inv.getMethodName())) {
       Controller cl=  inv.getController();
       User u=(User) cl.getSession().getAttribute("USER");
       if(Util.isNull(cl.getAttr("createUser"))){
    	   cl.setAttr("createUser", u.get("ID"));
    	   cl.setAttr("createUserName", u.get("userName"));
    	   cl.setAttr("createTime", u.get("createTime"));
       }
       cl.setAttr("modifyUser", u.get("ID"));
	   cl.setAttr("modifyUserName", u.get("userName"));
	   cl.setAttr("modifyTime", Util.getCurrentDate());
	}else{
		inv.invoke();
	}
	}
}
